package com.sx.entity.design;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.sx.basic.BasicEntity;
import com.sx.basic.serializer.LongToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;

/**
 * @author AdinZ
 * @date 2020/11/12 17:01
 * 页面关联组件
 */
@Data
@NoArgsConstructor
@TableName(value = "sx_design_page_module")
@Entity
@Table(name = "sx_design_page_module")
@ApiModel(description = "页面组件数据")
public class PageModuleEntity extends BasicEntity {
    @Id
    @GeneratedValue
    private Long id;//页面组件id
    @ApiModelProperty("关联页面id")
    @JSONField(serializeUsing = LongToStringSerializer.class)
    private long pageId;//页面id
    @ApiModelProperty("关联组件id")
    @JSONField(serializeUsing = LongToStringSerializer.class)
    private long moduleId; //组件id
    @ApiModelProperty("排序")
    private int sort;//组件排序
    @ApiModelProperty("组件变量")
    private String variable; //组件变量
    private long created=System.currentTimeMillis();

    @TableField(exist = false)@Transient
    @ApiModelProperty("组件参数json数据")
    private JSONObject data;

    @Column(columnDefinition = "text",name = "data")
    @TableField("data")
    @ApiModelProperty("data 的json数据")
    private String dataText;

    @ApiModelProperty("组件自定义名称")
    private String name; //组件自定义名称

    @ApiModelProperty("关联的组件模板")
    @TableField(exist = false)@Transient
    private DesignModuleEntity module;

    public JSONObject getData() {
        if(!StringUtils.isEmpty(this.dataText))
            this.data= JSON.parseObject(this.dataText);
        return data;
    }

    public String getDataText() {
        if(this.data!=null){
            this.dataText=this.data.toJSONString();
        }
        return dataText;
    }
}
